package com.nf.health.app.intervention_details.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.intervention_details.bean.DoctorOrderDetailsBean;
import com.nf.health.app.utils.TextUtilTools;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 恩福 on 2016/11/14.
 * 个人健康信息汇总 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class DoctorOrderDetailsAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEAD_INFO = 0;
    private static final int TYPE_PRESCRIPTION_DRUGS = 1;
    private static final int TYPE_AFTER_THE_HOSPITAL = 2;
    private static final int TYPE_REMARK = 3;
    private static final int TYPE_FOOTER = 4;
    private DoctorOrderDetailsBean doctorOrderDetailsBean;
    private Context mContext;

    public DoctorOrderDetailsAdapter(Context context, DoctorOrderDetailsBean doctorOrderDetailsBean) {
        this.mContext = context;
        this.doctorOrderDetailsBean = doctorOrderDetailsBean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEAD_INFO:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_doctor_details_head, parent, false);
                return new HeadInfoHolder(view);
            case TYPE_PRESCRIPTION_DRUGS:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_prescription_drugs, parent, false);
                return new PrescriptionDrugsHolder(view);
            case TYPE_AFTER_THE_HOSPITAL:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_after_the_hospital, parent, false);
                return new AfterTheHospitalHolder(view);
            case TYPE_REMARK:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_remark, parent, false);
                return new RemarkHolder(view);
            case TYPE_FOOTER:
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_foot_details, parent, false);
                return new FooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(doctorOrderDetailsBean == null) {
            return;
        }
        if (holder instanceof HeadInfoHolder) {
            HeadInfoHolder headInfoHolder = (HeadInfoHolder) holder;

            String str =
                    "<font color='#2E86F4'><big>诊疗医生</big></font>&nbsp;&nbsp; " + doctorOrderDetailsBean.getDepartment() + " &nbsp;&nbsp; " + doctorOrderDetailsBean.getDoctornick() + " <br>" +
                    "<font color='#2E86F4'><big>服务类型</big></font>&nbsp;&nbsp; " + doctorOrderDetailsBean.getHealthPackagename() + "<br>" +
                    "<font color='#2E86F4'><big>中医诊断</big></font>&nbsp;&nbsp; " + doctorOrderDetailsBean.getChinesediagnose() + " <br>" +
                    "<font color='#2E86F4'><big>西医诊断</big></font>&nbsp;&nbsp; " + doctorOrderDetailsBean.getWesterndiagnose() + " ";
            TextUtilTools.setTextFromHtml(headInfoHolder.tvDoctor, str);
        }

        if (holder instanceof PrescriptionDrugsHolder) {
            PrescriptionDrugsHolder prescriptionDrugsHolder = (PrescriptionDrugsHolder) holder;
            DoctorOrderPrescriptionDrugsAdapter doctorOrderPrescriptionDrugsAdapter = new DoctorOrderPrescriptionDrugsAdapter(mContext,doctorOrderDetailsBean.getMedication());
            prescriptionDrugsHolder.recyclerView.setAdapter(doctorOrderPrescriptionDrugsAdapter);
        }

        if (holder instanceof AfterTheHospitalHolder) {
            AfterTheHospitalHolder afterTheHospitalHolder = (AfterTheHospitalHolder) holder;
            DoctorOrderQuestionAdapter doctorOrderQuestionAdapter = new DoctorOrderQuestionAdapter(mContext,doctorOrderDetailsBean.getQuestionList());
            afterTheHospitalHolder.recyclerView.setAdapter(doctorOrderQuestionAdapter);
        }

        if (holder instanceof RemarkHolder) {
            RemarkHolder remarkHolder = (RemarkHolder) holder;
        }
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEAD_INFO;
            case 1:
                return TYPE_PRESCRIPTION_DRUGS;
            case 2:
                return TYPE_AFTER_THE_HOSPITAL;
            case 3:
                return TYPE_REMARK;
            case 4:
                return TYPE_FOOTER;
        }
        return super.getItemViewType(position);
    }


    class HeadInfoHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_doctor)
        TextView tvDoctor;

        public HeadInfoHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class PrescriptionDrugsHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.recycler_view_prescription_drugs)
        RecyclerView recyclerView;
        public PrescriptionDrugsHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            tvSubtitle.setText("用药处方");
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setFocusable(false);
        }
    }


    class AfterTheHospitalHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.recycler_view_question)
        RecyclerView recyclerView;

        public AfterTheHospitalHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            tvSubtitle.setText("院后");

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setFocusable(false);
        }
    }

    class RemarkHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.tv_cp)
        TextView tvRemark;

        public RemarkHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            tvSubtitle.setText("备注");
            tvRemark.setText(doctorOrderDetailsBean.getRemarks());
        }
    }

    class FooterHolder extends RecyclerView.ViewHolder {

        public FooterHolder(View itemView) {
            super(itemView);
        }
    }
}
